package rabbitMQ.direct;

import com.rabbitmq.client.*;
import rabbitMQ.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1Demo {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("124.223.31.81");
        factory.setPort(5672);
        factory.setVirtualHost("bite");
        factory.setUsername("study");
        factory.setPassword("study");
        Connection connection = factory.newConnection();
        //创建Channel
        Channel channel = connection.createChannel();
        //消费消息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收到的消息：" + new String(body));
            }
        };
        channel.basicConsume(Constants.DIRECT_QUEUE_NAME1,  true, consumer);


        //释放资源
       /* channel.close();;
        connection.close();*/
    }
}
